<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li class="active"><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. TCP">1. TCP</a><ul><li><a href="#t11.1 TCP服务器">1.1 TCP服务器</a><ul><li><a href="#t21.1.1 启动TCP服务器">1.1.1 启动TCP服务器</a></li><li><a href="#t31.1.2 使用TCP服务器">1.1.2 使用TCP服务器</a></li><li><a href="#t41.1.3 address">1.1.3 address</a></li><li><a href="#t51.1.4 getConnections">1.1.4 getConnections</a></li><li><a href="#t61.1.5 close">1.1.5 close</a></li></ul></li><li><a href="#t71.2 socket">1.2 socket</a><ul><li><a href="#t81.2.1 address">1.2.1 address</a></li><li><a href="#t91.2.2 读取数据">1.2.2 读取数据</a></li><li><a href="#t101.2.3 监听关闭事件">1.2.3 监听关闭事件</a></li><li><a href="#t111.2.4 pipe">1.2.4 pipe</a></li><li><a href="#t121.2.5 unpipe">1.2.5 unpipe</a></li><li><a href="#t131.2.5 pause&resume">1.2.5 pause&resume</a></li><li><a href="#t141.2.6 setTimeout">1.2.6 setTimeout</a></li></ul></li><li><a href="#t151.2 TCP客户端">1.2 TCP客户端</a><ul><li><a href="#t161.2.1 创建TCP客户端">1.2.1 创建TCP客户端</a></li><li><a href="#t171.2.2 向服务器端写入数据、end 、error、destroy,close">1.2.2 向服务器端写入数据、end 、error、destroy,close</a></li><li><a href="#t181.2.3 close">1.2.3 close</a></li><li><a href="#t191.2.4 unref&ref">1.2.4 unref&ref</a></li><li><a href="#t201.2.5 bufferSize">1.2.5 bufferSize</a></li><li><a href="#t211.2.6 keepAlive">1.2.6 keepAlive</a></li></ul></li></ul></li><li><a href="#t221.2.7 聊天室1.0">1.2.7 聊天室1.0</a></li><li><a href="#t231.2.8 聊天室2.0">1.2.8 聊天室2.0</a><ul><li><a href="#t241.3 类方法">1.3 类方法</a></li></ul></li><li><a href="#t252. UDP">2. UDP</a><ul><li><a href="#t262.1 创建socket">2.1 创建socket</a></li><li><a href="#t272.2 向外发送数据">2.2 向外发送数据</a></li><li><a href="#t282.3 address">2.3 address</a></li><li><a href="#t292.5 UDP服务器">2.5 UDP服务器</a></li><li><a href="#t302.6 UDP客户端">2.6 UDP客户端</a></li><li><a href="#t312.7 广播">2.7 广播</a><ul><li><a href="#t322.7.1 服务器">2.7.1 服务器</a></li><li><a href="#t332.7.2 客户端">2.7.2 客户端</a></li></ul></li><li><a href="#t342.8 组播">2.8 组播</a><ul><li><a href="#t352.8.1 服务器">2.8.1 服务器</a></li><li><a href="#t362.8.2 客户端">2.8.2 客户端</a></li></ul></li></ul></li><li><a href="#t37附录">附录</a><ul><li><a href="#t38软件">软件</a></li><li><a href="#t39telnet">telnet</a></li></ul></li><li><a href="#t40打开telnet功能">打开telnet功能</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. TCP">1. TCP <a href="#t01. TCP"> # </a></h2>
<p>&#x5728;Node.js&#x4E2D;&#xFF0C;&#x63D0;&#x4F9B;&#x4E86;net&#x6A21;&#x5757;&#x7528;&#x6765;&#x5B9E;&#x73B0;TCP&#x670D;&#x52A1;&#x5668;&#x548C;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x901A;&#x4FE1;&#x3002;</p>
<h3 id="t11.1 TCP&#x670D;&#x52A1;&#x5668;">1.1 TCP&#x670D;&#x52A1;&#x5668; <a href="#t11.1 TCP&#x670D;&#x52A1;&#x5668;"> # </a></h3>
<pre><code class="lang-javascript">net.createServer([options][, connectionListener])
</code></pre>
<ul>
<li>options.allowHalfOpen &#x662F;&#x5426;&#x5141;&#x8BB8;&#x5355;&#x65B9;&#x9762;&#x8FDE;&#x63A5;,&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;false</li>
<li>connectionListener&#x53C2;&#x6570;&#x7528;&#x4E8E;&#x6307;&#x5B9A;&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x65F6;&#x6240;&#x8981;&#x8C03;&#x7528;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;&#xFF0C;&#x56DE;&#x8C03;&#x4E2D;&#x6709;&#x4E00;&#x4E2A;&#x53C2;&#x6570;socket,&#x6307;&#x7684;&#x662F;TCP&#x670D;&#x52A1;&#x5668;&#x76D1;&#x542C;&#x7684;socket&#x7AEF;&#x53E3;&#x5BF9;&#x8C61;</li>
</ul>
<p>&#x4E5F;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x76D1;&#x542C;connection&#x4E8B;&#x4EF6;&#x7684;&#x65B9;&#x5F0F;&#x6765;&#x6307;&#x5B9A;&#x76D1;&#x542C;&#x51FD;&#x6570;</p>
<pre><code class="lang-javascript">server.on(<span class="hljs-string">&apos;connection&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">socket</span>)</span>{});
</code></pre>
<h4 id="t21.1.1 &#x542F;&#x52A8;TCP&#x670D;&#x52A1;&#x5668;">1.1.1 &#x542F;&#x52A8;TCP&#x670D;&#x52A1;&#x5668; <a href="#t21.1.1 &#x542F;&#x52A8;TCP&#x670D;&#x52A1;&#x5668;"> # </a></h4>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;listen&#x65B9;&#x6CD5;&#x901A;&#x77E5;&#x670D;&#x52A1;&#x5668;&#x5F00;&#x59CB;&#x76D1;&#x542C;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8FDE;&#x63A5;</p>
<pre><code class="lang-javascript">server.listen(port,[host],[backlog],[callback])
</code></pre>
<ul>
<li>port &#x5FC5;&#x987B;&#x6307;&#x5B9A;&#x7684;&#x7AEF;&#x53E3;&#x53F7;</li>
<li>host &#x6307;&#x5B9A;&#x9700;&#x8981;&#x76D1;&#x542C;&#x7684;IP&#x5730;&#x5740;&#x6216;&#x4E3B;&#x673A;&#x540D;&#xFF0C;&#x5982;&#x679C;&#x7701;&#x7565;&#x7684;&#x8BDD;&#x670D;&#x52A1;&#x5668;&#x5C06;&#x76D1;&#x542C;&#x6765;&#x81EA;&#x4E8E;&#x4EFB;&#x4F55;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8FDE;&#x63A5;</li>
<li>backlog&#x6307;&#x5B9A;&#x4F4D;&#x4E8E;&#x7B49;&#x5F85;&#x961F;&#x5217;&#x4E2D;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x7684;&#x6700;&#x5927;&#x6570;&#x91CF;&#xFF0C;&#x9ED8;&#x8BA4;&#x503C;&#x4E3A;511</li>
</ul>
<pre><code class="lang-javascript">server.on(<span class="hljs-string">&apos;listening&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{});
</code></pre>
<h4 id="t31.1.2 &#x4F7F;&#x7528;TCP&#x670D;&#x52A1;&#x5668;">1.1.2 &#x4F7F;&#x7528;TCP&#x670D;&#x52A1;&#x5668; <a href="#t31.1.2 &#x4F7F;&#x7528;TCP&#x670D;&#x52A1;&#x5668;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">socket</span>)</span>{
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x5DF2;&#x8FDE;&#x63A5;&apos;</span>);
});
server.listen(<span class="hljs-number">8080</span>,<span class="hljs-string">&apos;localhost&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x670D;&#x52A1;&#x5668;&#x5F00;&#x59CB;&#x76D1;&#x542C;&apos;</span>);
});
</code></pre>
<h4 id="t41.1.3 address">1.1.3 address <a href="#t41.1.3 address"> # </a></h4>
<pre><code class="lang-javascript">server.address()
</code></pre>
<ul>
<li>port &#x7AEF;&#x53E3;&#x53F7;</li>
<li>address TCP&#x670D;&#x52A1;&#x5668;&#x76D1;&#x542C;&#x7684;&#x5730;&#x5740;</li>
<li>family &#x534F;&#x8BAE;&#x7684;&#x7248;&#x672C;</li>
</ul>
<h4 id="t51.1.4 getConnections">1.1.4 getConnections <a href="#t51.1.4 getConnections"> # </a></h4>
<p>&#x67E5;&#x770B;&#x5F53;&#x524D;&#x4E0E;TCP&#x670D;&#x52A1;&#x5668;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8FDE;&#x63A5;&#x6570;&#x91CF;&#x4EE5;&#x53CA;&#x8BBE;&#x7F6E;&#x6700;&#x5927;&#x8FDE;&#x63A5;&#x6570;&#x91CF;</p>
<pre><code class="lang-javascript">server.getConnections(callback);
 server.maxConnections = <span class="hljs-number">2</span>;
</code></pre>
<h4 id="t61.1.5 close">1.1.5 close <a href="#t61.1.5 close"> # </a></h4>
<p>&#x4F7F;&#x7528;close&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x663E;&#x5F0F;&#x62D2;&#x7EDD;&#x6240;&#x6709;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x7684;&#x8FDE;&#x63A5;&#x8BF7;&#x6C42;,&#x5F53;&#x6240;&#x6709;&#x5DF2;&#x8FDE;&#x63A5;&#x7684;&#x5BA2;&#x6237;&#x7AEF;&#x5173;&#x95ED;&#x540E;&#x670D;&#x52A1;&#x5668;&#x4F1A;&#x81EA;&#x52A8;&#x5173;&#x95ED;,&#x5E76;&#x89E6;&#x53D1;&#x670D;&#x52A1;&#x5668;&#x7684;close&#x4E8B;&#x4EF6;&#x3002;</p>
<pre><code class="lang-javascript">server.close();
server.on(<span class="hljs-string">&apos;close&apos;</span>,callback);
</code></pre>
<h3 id="t71.2 socket">1.2 socket <a href="#t71.2 socket"> # </a></h3>
<h4 id="t81.2.1 address">1.2.1 address <a href="#t81.2.1 address"> # </a></h4>
<p>net.Socket&#x4EE3;&#x8868;&#x4E00;&#x4E2A;socket&#x7AEF;&#x53E3;&#x5BF9;&#x8C61;,&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x53EF;&#x8BFB;&#x53EF;&#x5199;&#x6D41;&#x3002;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">let</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;util&apos;</span>);
<span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">socket</span>)</span>{
  server.getConnections(<span class="hljs-function">(<span class="hljs-params">err,count</span>)=&gt;</span>{
      server.maxConnections = <span class="hljs-number">1</span>;
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x6700;&#x5927;&#x8FDE;&#x63A5;&#x6570;&#x91CF;%d,&#x5F53;&#x524D;&#x8FDE;&#x63A5;&#x6570;&#x91CF;%d&apos;</span>,server.maxConnections,count); 
  });  
  <span class="hljs-keyword">let</span> address = socket.address();
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x5730;&#x5740; %s&apos;</span>,util.inspect(address));
});
</code></pre>
<h4 id="t91.2.2 &#x8BFB;&#x53D6;&#x6570;&#x636E;">1.2.2 &#x8BFB;&#x53D6;&#x6570;&#x636E; <a href="#t91.2.2 &#x8BFB;&#x53D6;&#x6570;&#x636E;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    socket.setEncoding(<span class="hljs-string">&apos;utf8&apos;</span>);
    socket.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x672C;&#x6B21;&#x6536;&#x5230;&#x7684;&#x5185;&#x5BB9;&#x4E3A;%s,&#x7D2F;&#x8BA1;&#x6536;&#x5230;&#x7684;&#x5B57;&#x8282;&#x6570;&#x662F;%d&apos;</span>, data, socket.bytesRead);
    });
});
</code></pre>
<h4 id="t101.2.3 &#x76D1;&#x542C;&#x5173;&#x95ED;&#x4E8B;&#x4EF6;">1.2.3 &#x76D1;&#x542C;&#x5173;&#x95ED;&#x4E8B;&#x4EF6; <a href="#t101.2.3 &#x76D1;&#x542C;&#x5173;&#x95ED;&#x4E8B;&#x4EF6;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    socket.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x5DF2;&#x7ECF;&#x5173;&#x95ED;&apos;</span>);
    });
});
</code></pre>
<h4 id="t111.2.4 pipe">1.2.4 pipe <a href="#t111.2.4 pipe"> # </a></h4>
<p>pipe&#x65B9;&#x6CD5;&#x53EF;&#x4EE5;&#x5C06;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#x5199;&#x5230;&#x6587;&#x4EF6;&#x6216;&#x5176;&#x5B83;&#x76EE;&#x6807;&#x4E2D;&#x3002;</p>
<pre><code class="lang-javascript">socket.pipe(destinatin,[options]);
</code></pre>
<ul>
<li>options.end &#x8BBE;&#x7F6E;&#x4E3A;false&#x65F6;&#x5F53;&#x5BA2;&#x6237;&#x7AEF;&#x7ED3;&#x675F;&#x5199;&#x64CD;&#x4F5C;&#x6216;&#x5173;&#x95ED;&#x540E;&#x5E76;&#x4E0D;&#x4F1A;&#x5173;&#x95ED;&#x76EE;&#x6807;&#x5BF9;&#x8C61;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x7EE7;&#x7EED;&#x5199;&#x5165;&#x6570;&#x636E;</li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> ws = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>).createWriteStream(path.resolve(__dirname, <span class="hljs-string">&apos;msg.txt&apos;</span>));
<span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    socket.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-built_in">console</span>.log(data);
    });
    socket.pipe(ws, { <span class="hljs-attr">end</span>: <span class="hljs-literal">false</span> });
    socket.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        ws.end(<span class="hljs-string">&apos;over&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            socket.unpipe(ws);
        });
    });
});
</code></pre>
<h4 id="t121.2.5 unpipe">1.2.5 unpipe <a href="#t121.2.5 unpipe"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> file = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>).createWriteStream(path.join(__dirname, <span class="hljs-string">&apos;msg.txt&apos;</span>));
<span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    socket.pipe(file, {
        <span class="hljs-attr">end</span>: <span class="hljs-literal">false</span>
    });
    setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        file.end(<span class="hljs-string">&apos;bye bye&apos;</span>);
        socket.unpipe(file);
    }, <span class="hljs-number">5000</span>);
    <span class="hljs-comment">// socket.on(&apos;end&apos;, function () {</span>
    <span class="hljs-comment">//     file.end(&apos;bye bye&apos;);</span>
    <span class="hljs-comment">// });</span>
});
server.listen(<span class="hljs-number">8080</span>);
</code></pre>
<h4 id="t131.2.5 pause&amp;resume">1.2.5 pause&amp;resume <a href="#t131.2.5 pause&amp;resume"> # </a></h4>
<p><code>pause</code>&#x53EF;&#x4EE5;&#x6682;&#x505C;<code>data</code>&#x4E8B;&#x4EF6;&#x89E6;&#x53D1;&#xFF0C;&#x670D;&#x52A1;&#x5668;&#x4F1A;&#x628A;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#x6682;&#x5B58;&#x5728;&#x7F13;&#x5B58;&#x533A;&#x91CC;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">const</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> file = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>).createWriteStream(path.join(__dirname, <span class="hljs-string">&apos;msg.txt&apos;</span>));
<span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    socket.pause();
    setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        socket.resume();
        socket.pipe(file);
    }, <span class="hljs-number">10</span> * <span class="hljs-number">1000</span>);
});
server.listen(<span class="hljs-number">8080</span>);
</code></pre>
<h4 id="t141.2.6 setTimeout">1.2.6 setTimeout <a href="#t141.2.6 setTimeout"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">let</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">let</span> ws = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>).createWriteStream(path.resolve(__dirname, <span class="hljs-string">&apos;msg.txt&apos;</span>));
<span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    socket.setTimeout(<span class="hljs-number">5</span> * <span class="hljs-number">1000</span>);
    socket.pause();
    socket.on(<span class="hljs-string">&apos;timeout&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        socket.pipe(ws);
    });

    <span class="hljs-comment">//socket.setTimeout(0);&#x53D6;&#x6D88;&#x8D85;&#x65F6;&#x65F6;&#x95F4;&#x7684;&#x8BBE;&#x7F6E;</span>
});
server.listen(<span class="hljs-number">8080</span>);
</code></pre>
<h3 id="t151.2 TCP&#x5BA2;&#x6237;&#x7AEF;">1.2 TCP&#x5BA2;&#x6237;&#x7AEF; <a href="#t151.2 TCP&#x5BA2;&#x6237;&#x7AEF;"> # </a></h3>
<h4 id="t161.2.1 &#x521B;&#x5EFA;TCP&#x5BA2;&#x6237;&#x7AEF;">1.2.1 &#x521B;&#x5EFA;TCP&#x5BA2;&#x6237;&#x7AEF; <a href="#t161.2.1 &#x521B;&#x5EFA;TCP&#x5BA2;&#x6237;&#x7AEF;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> socket = <span class="hljs-keyword">new</span> net.Socket([options])
</code></pre>
<ul>
<li>fd socket&#x6587;&#x4EF6;&#x63CF;&#x8FF0;&#x7B26;</li>
<li>type &#x5BA2;&#x6237;&#x7AEF;&#x6240;&#x6709;&#x534F;&#x8BAE;</li>
<li>allowHalfOpen &#x662F;&#x5426;&#x5141;&#x8BB8;&#x534A;&#x8FDE;&#x63A5;,&#x670D;&#x52A1;&#x5668;&#x6536;&#x5230;FIN&#x5305;&#x65F6;&#x4E0D;&#x56DE;&#x53D1;FIN&#x5305;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x670D;&#x52A1;&#x5668;&#x53EF;&#x4EE5;&#x7EE7;&#x7EED;&#x5411;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x6570;&#x636E;</li>
</ul>
<pre><code class="lang-javascript">socket.connect(port, host, callback);
socket.on(<span class="hljs-string">&apos;connect&apos;</span>, callback);
</code></pre>
<h4 id="t171.2.2 &#x5411;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5199;&#x5165;&#x6570;&#x636E;&#x3001;end &#x3001;error&#x3001;destroy,close">1.2.2 &#x5411;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5199;&#x5165;&#x6570;&#x636E;&#x3001;end &#x3001;error&#x3001;destroy,close <a href="#t171.2.2 &#x5411;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5199;&#x5165;&#x6570;&#x636E;&#x3001;end &#x3001;error&#x3001;destroy,close"> # </a></h4>
<ul>
<li>write&#x8868;&#x793A;&#x5411;&#x670D;&#x52A1;&#x5668;&#x5199;&#x5165;&#x6570;&#x636E;</li>
<li>end &#x7528;&#x4E8E;&#x7ED3;&#x675F;&#x8FDE;&#x63A5;</li>
<li>error &#x8FDE;&#x63A5;&#x53D1;&#x751F;&#x9519;&#x8BEF;</li>
<li>destroy &#x9500;&#x6BC1;&#x6D41;</li>
<li>close &#x8868;&#x793A;&#x8FDE;&#x63A5;&#x5173;&#x95ED;&#x6210;&#x529F;&#xFF0C;hasError=true&#x4EE3;&#x8868;&#x6709;&#x53EF;&#x80FD;&#x6709;&#x9519;&#x8BEF;<pre><code>socket.write(data,[encoding],[callback]);
</code></pre></li>
</ul>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5BA2;&#x6237;&#x7AEF;&#x5DF2;&#x7ECF;&#x8FDE;&#x63A5;&quot;</span>);
    socket.setEncoding(<span class="hljs-string">&apos;utf8&apos;</span>);
    socket.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5DF2;&#x63A5;&#x6536;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;:%s&quot;</span>, data);
        socket.write(<span class="hljs-string">&apos;&#x670D;&#x52A1;&#x5668;:&apos;</span> + data);
    })
    socket.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x4E0E;&#x5BA2;&#x6237;&#x7AEF;&#x901A;&#x4FE1;&#x8FC7;&#x7A0B;&#x4E2D;&#x53D1;&#x751F;&#x4E86;&#x9519;&#x8BEF;&#xFF0C;&#x9519;&#x8BEF;&#x7F16;&#x7801;&#x4E3A;%s&apos;</span>, err.code);
        socket.destroy();
    });
    socket.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x5DF2;&#x7ECF;&#x5173;&#x95ED;&#x8FDE;&#x63A5;&apos;</span>);
        socket.destroy();
    });
    socket.on(<span class="hljs-string">&apos;close&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">hasError</span>) </span>{
        <span class="hljs-built_in">console</span>.log(hasError ? <span class="hljs-string">&apos;&#x5F02;&#x5E38;&#x5173;&#x95ED;&apos;</span> : <span class="hljs-string">&apos;&#x6B63;&#x5E38;&#x5173;&#x95ED;&apos;</span>);
    });
});
server.listen(<span class="hljs-number">808</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">let</span> client = <span class="hljs-keyword">new</span> net.Socket();
    client.setEncoding(<span class="hljs-string">&apos;utf8&apos;</span>);
    client.connect(<span class="hljs-number">808</span>, <span class="hljs-string">&apos;127.0.0.1&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x5DF2;&#x8FDE;&#x63A5;&apos;</span>);
        client.write(<span class="hljs-string">&apos;hello&apos;</span>);
        setTimeout(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
            client.end(<span class="hljs-string">&apos;byebye&apos;</span>);
        }, <span class="hljs-number">5000</span>);
    });
    client.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5DF2;&#x7ECF;&#x63A5;&#x6536;&#x5230;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x8FC7;&#x6765;&#x7684;&#x6570;&#x636E;:%s&apos;</span>, data);
    });
    client.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x901A;&#x4FE1;&#x8FC7;&#x7A0B;&#x4E2D;&#x53D1;&#x751F;&#x4E86;&#x9519;&#x8BEF;&#xFF0C;&#x9519;&#x8BEF;&#x7F16;&#x7801;&#x4E3A;%s&apos;</span>, err.code);
        client.destroy();
    });

});
</code></pre>
<h4 id="t181.2.3 close">1.2.3 close <a href="#t181.2.3 close"> # </a></h4>
<p>&#x505C;&#x6B62;server&#x63A5;&#x53D7;&#x5EFA;&#x7ACB;&#x65B0;&#x7684;connections&#x5E76;&#x4FDD;&#x6301;&#x5DF2;&#x7ECF;&#x5B58;&#x5728;&#x7684;connections</p>
<pre><code class="lang-javascript">  server.getConnections(<span class="hljs-function">(<span class="hljs-params">err, count</span>) =&gt;</span> {
      <span class="hljs-keyword">if</span> (count == <span class="hljs-number">2</span>) server.close();
 });
</code></pre>
<h4 id="t191.2.4 unref&amp;ref">1.2.4 unref&amp;ref <a href="#t191.2.4 unref&amp;ref"> # </a></h4>
<p>unref&#x65B9;&#x6CD5;&#x6307;&#x5B9A;&#x53D1;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x88AB;&#x5168;&#x90E8;&#x5173;&#x95ED;&#x65F6;&#x9000;&#x51FA;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;
 &#x5982;&#x679C;&#x5C06;allowHalfOpen&#x65B9;&#x6CD5;&#xFF0C;&#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x4E0E;&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x7684;socket&#x7AEF;&#x53E3;&#x5BF9;&#x8C61;&#x7684;end &#x65B9;&#x6CD5;&#x4E3B;&#x52A8;&#x5173;&#x95ED;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x8FDE;&#x63A5;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">let</span> server = net.createServer({ <span class="hljs-attr">allowHalfOpen</span>: <span class="hljs-literal">true</span> }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5BA2;&#x6237;&#x7AEF;&#x5DF2;&#x7ECF;&#x8FDE;&#x63A5;&quot;</span>);
    socket.setEncoding(<span class="hljs-string">&apos;utf8&apos;</span>);
    socket.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5DF2;&#x63A5;&#x6536;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;:%s&quot;</span>, data);
        socket.write(<span class="hljs-string">&apos;&#x670D;&#x52A1;&#x5668;&#x786E;&#x8BA4;&#x6570;&#x636E;:&apos;</span> + data);
    })
    socket.on(<span class="hljs-string">&apos;error&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x4E0E;&#x5BA2;&#x6237;&#x7AEF;&#x901A;&#x4FE1;&#x8FC7;&#x7A0B;&#x4E2D;&#x53D1;&#x751F;&#x4E86;&#x9519;&#x8BEF;&#xFF0C;&#x9519;&#x8BEF;&#x7F16;&#x7801;&#x4E3A;%s&apos;</span>, err.code);
        socket.destroy();
    });
    socket.on(<span class="hljs-string">&apos;end&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5BA2;&#x6237;&#x7AEF;&#x5DF2;&#x7ECF;&#x5173;&#x95ED;&#x8FDE;&#x63A5;&apos;</span>);
        socket.end();
        server.unref();
    });
    socket.on(<span class="hljs-string">&apos;close&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">hasError</span>) </span>{
        <span class="hljs-keyword">if</span> (hasError) {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7531;&#x4E8E;&#x9519;&#x8BEF;&#x5BFC;&#x81F4;socket&#x5173;&#x95ED;&apos;</span>);
            server.unref();
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7AEF;&#x53E3;&#x6B63;&#x5E38;&#x5173;&#x95ED;&apos;</span>);
        }
    });
    server.getConnections(<span class="hljs-function">(<span class="hljs-params">err, count</span>) =&gt;</span> {
        <span class="hljs-keyword">if</span> (count == <span class="hljs-number">2</span>) server.close();
    });
});
server.listen(<span class="hljs-number">808</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{ });
server.on(<span class="hljs-string">&apos;close&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x670D;&#x52A1;&#x5668;&#x5173;&#x95ED;&apos;</span>);
});
</code></pre>
<h4 id="t201.2.5 bufferSize">1.2.5 bufferSize <a href="#t201.2.5 bufferSize"> # </a></h4>
<p>write&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x548C;bufferSize&#x5C5E;&#x6027;&#x503C;</p>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> server = net.createServer({ <span class="hljs-attr">allowHalfOpen</span>: <span class="hljs-literal">true</span> }, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x5BA2;&#x6237;&#x7AEF;&#x5DF2;&#x7ECF;&#x8FDE;&#x63A5;&quot;</span>);
    socket.setEncoding(<span class="hljs-string">&apos;utf8&apos;</span>);
    <span class="hljs-keyword">let</span> rs = fs.createReadStream(path.resolve(__dirname, <span class="hljs-string">&apos;a.txt&apos;</span>), { <span class="hljs-attr">highWaterMark</span>: <span class="hljs-number">2</span> });
    rs.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-keyword">let</span> flag = socket.write(data);
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;flag:&quot;</span>, flag);
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x7F13;&#x5B58;&#x5B57;&#x8282;:&apos;</span> + socket.bufferSize);
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x5DF2;&#x53D1;&#x9001;&#x5B57;&#x8282;:&apos;</span> + socket.bytesWritten);
    })
    socket.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;data&apos;</span>, data);
    });
    socket.on(<span class="hljs-string">&apos;drain&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err</span>) </span>{
        <span class="hljs-string">&quot;&#x7F13;&#x5B58;&#x533A;&#x5DF2;&#x5168;&#x90E8;&#x53D1;&#x9001;&quot;</span>
    });
});
</code></pre>
<h4 id="t211.2.6 keepAlive">1.2.6 keepAlive <a href="#t211.2.6 keepAlive"> # </a></h4>
<p>&#x5F53;&#x670D;&#x52A1;&#x5668;&#x548C;&#x5BA2;&#x6237;&#x7AEF;&#x5EFA;&#x7ACB;&#x8FDE;&#x63A5;&#x540E;&#xFF0C;&#x5F53;&#x4E00;&#x65B9;&#x4E3B;&#x673A;&#x7A81;&#x7136;&#x65AD;&#x7535;&#x3001;&#x91CD;&#x542F;&#x3001;&#x7CFB;&#x7EDF;&#x5D29;&#x6E83;&#x7B49;&#x610F;&#x5916;&#x60C5;&#x51B5;&#x65F6;&#xFF0C;&#x5C06;&#x6765;&#x4E0D;&#x53CA;&#x5411;&#x53E6;&#x4E00;&#x65B9;&#x53D1;&#x9001;FIN&#x5305;&#xFF0C;&#x8FD9;&#x6837;&#x53E6;&#x4E00;&#x65B9;&#x5C06;&#x6C38;&#x8FDC;&#x5904;&#x4E8E;&#x8FDE;&#x63A5;&#x72B6;&#x6001;&#x3002;
&#x53EF;&#x4EE5;&#x4F7F;&#x7528;setKeepAlive&#x65B9;&#x6CD5;&#x6765;&#x89E3;&#x51B3;&#x8FD9;&#x4E00;&#x4E2A;&#x95EE;&#x9898;</p>
<pre><code>socket.setKeepAlive([enaable],[initialDelay]);
</code></pre><ul>
<li>enable &#x662F;&#x5426;&#x542F;&#x7528;&#x55C5;&#x63A2;&#xFF0C;&#x4E3A;true&#x65F6;&#x4F1A;&#x4E0D;&#x4F46;&#x5411;&#x5BF9;&#x65B9;&#x53D1;&#x9001;&#x63A2;&#x6D4B;&#x5305;&#xFF0C;&#x6CA1;&#x6709;&#x54CD;&#x5E94;&#x5219;&#x8BA4;&#x4E3A;&#x5BF9;&#x65B9;&#x5DF2;&#x7ECF;&#x5173;&#x95ED;&#x8FDE;&#x63A5;&#xFF0C;&#x81EA;&#x5DF1;&#x5219;&#x5173;&#x95ED;&#x8FDE;&#x63A5;</li>
<li>initialDelay  &#x591A;&#x4E45;&#x53D1;&#x9001;&#x4E00;&#x6B21;&#x63A2;&#x6D4B;&#x5305;&#xFF0C;&#x5355;&#x4F4D;&#x662F;&#x6BEB;&#x79D2;</li>
</ul>
<h2 id="t221.2.7 &#x804A;&#x5929;&#x5BA4;1.0">1.2.7 &#x804A;&#x5929;&#x5BA4;1.0 <a href="#t221.2.7 &#x804A;&#x5929;&#x5BA4;1.0"> # </a></h2>
<pre><code class="lang-javascript"><span class="hljs-comment">/**
 * 1.&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x670D;&#x52A1;&#x5668;
 * 2. &#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x8FDE;&#x63A5;&#x670D;&#x52A1;&#x5668;
 * 3.&#x5BA2;&#x6237;&#x7AEF;&#x53EF;&#x4EE5;&#x53D1;&#x8A00;&#xFF0C;&#x7136;&#x540E;&#x5E7F;&#x64AD;&#x7ED9;&#x5927;&#x5BB6;
 * 4.&#x5BA2;&#x6237;&#x7AEF;&#x8FDE;&#x63A5;&#x548C;&#x9000;&#x51FA;&#x540E;&#x90FD;&#x8981;&#x901A;&#x77E5;&#x5927;&#x5BB6;&#x3002;
 * 5.&#x663E;&#x793A;&#x5F53;&#x524D;&#x7684;&#x5728;&#x7EBF;&#x4EBA;&#x6570;
 */</span>
<span class="hljs-keyword">let</span> net = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;net&apos;</span>);
<span class="hljs-keyword">let</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;util&apos;</span>);
<span class="hljs-keyword">let</span> clients = {};
<span class="hljs-keyword">let</span> server = net.createServer(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">socket</span>) </span>{
    server.getConnections(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">err, count</span>) </span>{
        socket.write(<span class="hljs-string">`weclome,there is <span class="hljs-subst">${count}</span> users now,please input your username\r\n`</span>);
    });
    <span class="hljs-keyword">let</span> nickname;
    socket.setEncoding(<span class="hljs-string">&apos;utf8&apos;</span>);
    socket.on(<span class="hljs-string">&apos;data&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">data</span>) </span>{
        data = data.replace(<span class="hljs-regexp">/\r\n/</span>, <span class="hljs-string">&apos;&apos;</span>);
        <span class="hljs-keyword">if</span> (data == <span class="hljs-string">&apos;byebye&apos;</span>) {
            socket.end();
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">if</span> (nickname) {
                broadcast(nickname, <span class="hljs-string">`<span class="hljs-subst">${nickname}</span>:<span class="hljs-subst">${data}</span>`</span>);
            } <span class="hljs-keyword">else</span> {
                nickname = data;
                clients[nickname] = socket;
                broadcast(nickname, <span class="hljs-string">`welcome <span class="hljs-subst">${nickname}</span> joined us!`</span>);
            }
        }

    });
    socket.on(<span class="hljs-string">&apos;close&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
        socket.destroy();
    });
}).listen(<span class="hljs-number">8088</span>);

<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">broadcast</span>(<span class="hljs-params">nickname, msg</span>) </span>{
    <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> key <span class="hljs-keyword">in</span> clients) {
        <span class="hljs-keyword">if</span> (key != nickname) {
            clients[key].write(msg + <span class="hljs-string">&apos;\r\n&apos;</span>);
            clients[nickname].destroy();
            <span class="hljs-keyword">delete</span> clients[nickname];
        }
    }
}
</code></pre>
<h2 id="t231.2.8 &#x804A;&#x5929;&#x5BA4;2.0">1.2.8 &#x804A;&#x5929;&#x5BA4;2.0 <a href="#t231.2.8 &#x804A;&#x5929;&#x5BA4;2.0"> # </a></h2>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> key = scoket.remoteAddress+<span class="hljs-string">&apos;:&apos;</span>+socket.remotePort;
users[key] = {<span class="hljs-attr">name</span>:<span class="hljs-string">&apos;&#x533F;&#x540D;&apos;</span>,socket};

socket.on(<span class="hljs-string">&apos;data&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    parse(data);
});
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">parse</span>(<span class="hljs-params">msg</span>)</span>{
  swtich(msg.type){
   <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;secret&apos;</span>:
     secret(msg.user,msg.text);
     <span class="hljs-keyword">break</span>;
  }
  <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;boardcast&apos;</span>:
     boardcast(message.text);
     <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;cname&apos;</span>:
     cname(messsage.text);
     <span class="hljs-keyword">break</span>;
  <span class="hljs-keyword">case</span> <span class="hljs-string">&apos;list&apos;</span>:
     list();
     <span class="hljs-keyword">break</span>;   
  <span class="hljs-keyword">default</span>:
     socket.write(<span class="hljs-string">&apos;&#x4E0D;&#x80FD;&#x8BC6;&#x522B;&#x547D;&#x4EE4;&apos;</span>);
     <span class="hljs-keyword">break</span>;
}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">secret</span>(<span class="hljs-params">user,text</span>)</span>{

}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">boardcast</span>(<span class="hljs-params">text</span>)</span>{

}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">cname</span>(<span class="hljs-params">text</span>)</span>{

}
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">list</span>(<span class="hljs-params"></span>)</span>{

}
</code></pre>
<pre><code class="lang-javascript">b:text &#x5E7F;&#x64AD;
c:nickname:text &#x79C1;&#x804A;
n:nickname &#x6539;&#x540D;
l &#x5217;&#x51FA;&#x5728;&#x7EBF;&#x7528;&#x6237;&#x5217;&#x8868;

on(&apos;data&apos;,function(data){
   if(data == &apos;quit){

   }else if(data == &apos;help&apos;){

   }else(){
     write(data);
   }
});
function convert(){

}
</code></pre>
<h3 id="t241.3 &#x7C7B;&#x65B9;&#x6CD5;">1.3 &#x7C7B;&#x65B9;&#x6CD5; <a href="#t241.3 &#x7C7B;&#x65B9;&#x6CD5;"> # </a></h3>
<ul>
<li>isIP &#x5224;&#x65AD;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x662F;IP</li>
<li>isIPv4 &#x5224;&#x65AD;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x662F;IPv4&#x5730;&#x5740;</li>
<li>isIPv6 &#x5224;&#x65AD;&#x5B57;&#x7B26;&#x4E32;&#x662F;&#x5426;&#x662F;IPv6&#x5730;&#x5740;</li>
</ul>
<h2 id="t252. UDP">2. UDP <a href="#t252. UDP"> # </a></h2>
<h3 id="t262.1 &#x521B;&#x5EFA;socket">2.1 &#x521B;&#x5EFA;socket <a href="#t262.1 &#x521B;&#x5EFA;socket"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> socket = dgram.createSocket(type,[callback]);
socket.on(<span class="hljs-string">&apos;messsage&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">msg,rinfo</span>)</span>{});
</code></pre>
<ul>
<li>type &#x5FC5;&#x987B;&#x8F93;&#x5165;&#xFF0C;&#x5236;&#x5B9A;&#x65F6;udp4&#x8FD8;&#x662F;udp6</li>
<li>callback &#x4ECE;&#x8BE5;&#x63A5;&#x53E3;&#x63A5;&#x6536;&#x5230;&#x6570;&#x636E;&#x65F6;&#x8C03;&#x7528;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;<ul>
<li>msg &#x63A5;&#x6536;&#x5230;&#x7684;&#x6570;&#x636E;</li>
<li>rinfo &#x4FE1;&#x606F;&#x5BF9;&#x8C61;<ul>
<li>address &#x53D1;&#x9001;&#x7740;&#x7684;&#x5730;&#x5740;</li>
<li>family ipv4&#x8FD8;&#x662F;ipv6</li>
<li>port &#x53D1;&#x9001;&#x8005;&#x7684;socket&#x7AEF;&#x53E3;&#x53F7;</li>
<li>size &#x53D1;&#x9001;&#x8005;&#x6240;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#x5B57;&#x8282;&#x6570;</li>
</ul>
</li>
</ul>
</li>
</ul>
<pre><code class="lang-javascript">socket.bind(port,[address],[callback]);
socket.on(<span class="hljs-string">&apos;listening&apos;</span>,callabck;
</code></pre>
<ul>
<li>port &#x7ED1;&#x5B9A;&#x7684;&#x7AEF;&#x53E3;&#x53F7;</li>
<li>address &#x76D1;&#x542C;&#x7684;&#x5730;&#x5740;</li>
<li>callback &#x76D1;&#x542C;&#x6210;&#x529F;&#x540E;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;</li>
</ul>
<h3 id="t272.2 &#x5411;&#x5916;&#x53D1;&#x9001;&#x6570;&#x636E;">2.2 &#x5411;&#x5916;&#x53D1;&#x9001;&#x6570;&#x636E; <a href="#t272.2 &#x5411;&#x5916;&#x53D1;&#x9001;&#x6570;&#x636E;"> # </a></h3>
<p> &#x5982;&#x679C;&#x53D1;&#x9001;&#x6570;&#x636E;&#x524D;&#x8FD8;&#x6CA1;&#x6709;&#x7ED1;&#x5B9A;&#x8FC7;&#x5730;&#x5740;&#x548C;&#x7AEF;&#x53E3;&#x53F7;&#xFF0C;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x5C06;&#x4E3A;&#x5176;&#x5206;&#x914D;&#x4E00;&#x4E2A;&#x968F;&#x673A;&#x7AEF;&#x53E3;&#x5E76;&#x53EF;&#x4EE5;&#x63A5;&#x6536;&#x4EFB;&#x4F55;&#x5730;&#x5740;&#x7684;&#x6570;&#x636E;</p>
<pre><code class="lang-javascript"> socket.send(buf,offset,length,port,address,[callback]);
</code></pre>
<ul>
<li>buffer &#x4EE3;&#x8868;&#x7F13;&#x5B58;&#x533A;</li>
<li>offset &#x4ECE;&#x7F13;&#x5B58;&#x533A;&#x7B2C;&#x51E0;&#x4E2A;&#x5B57;&#x8282;&#x5F00;&#x59CB;&#x53D1;</li>
<li>length &#x8981;&#x53D1;&#x9001;&#x7684;&#x5B57;&#x8282;&#x6570;</li>
<li>port &#x5BF9;&#x65B9;&#x7684;&#x7AEF;&#x53E3;&#x53F7;</li>
<li>address &#x63A5;&#x6536;&#x6570;&#x636E;&#x7684;socket&#x5730;&#x5740;</li>
<li><p>callback &#x5236;&#x5B9A;&#x5F53;&#x6570;&#x636E;&#x53D1;&#x9001;&#x5B8C;&#x6BD5;&#x65F6;&#x6240;&#x9700;&#x8981;&#x7684;&#x56DE;&#x8C03;&#x51FD;&#x6570;</p>
<ul>
<li>err &#x9519;&#x8BEF;&#x5BF9;&#x8C61;</li>
<li>byets &#x5B9E;&#x9645;&#x53D1;&#x9001;&#x7684;&#x5B57;&#x8282;&#x6570;</li>
</ul>
<h3 id="t282.3 address">2.3 address <a href="#t282.3 address"> # </a></h3>
<p>&#x83B7;&#x53D6;&#x6B64;socket&#x76F8;&#x5173;&#x7684;&#x5730;&#x5740;&#x4FE1;&#x606F;</p>
<pre><code>let address = socket.address();
</code></pre></li>
<li>port</li>
<li>address</li>
<li>family</li>
</ul>
<h3 id="t292.5 UDP&#x670D;&#x52A1;&#x5668;">2.5 UDP&#x670D;&#x52A1;&#x5668; <a href="#t292.5 UDP&#x670D;&#x52A1;&#x5668;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> dgram = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;dgram&apos;</span>);
<span class="hljs-keyword">var</span> socket = dgram.createSocket(<span class="hljs-string">&apos;udp4&apos;</span>);
socket.on(<span class="hljs-string">&apos;message&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">msg,rinfo</span>)</span>{
  <span class="hljs-built_in">console</span>.log(msg.toString());
  <span class="hljs-built_in">console</span>.log(rinfo);
   socket.send(msg,<span class="hljs-number">0</span>,msg.length,rinfo.port,rinfo.address);
});
socket.bind(<span class="hljs-number">41234</span>,<span class="hljs-string">&apos;localhost&apos;</span>);
</code></pre>
<h3 id="t302.6 UDP&#x5BA2;&#x6237;&#x7AEF;">2.6 UDP&#x5BA2;&#x6237;&#x7AEF; <a href="#t302.6 UDP&#x5BA2;&#x6237;&#x7AEF;"> # </a></h3>
<pre><code class="lang-javascript"><span class="hljs-keyword">var</span> dgram = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;dgram&apos;</span>);
<span class="hljs-keyword">var</span> socket = dgram.createSocket(<span class="hljs-string">&apos;udp4&apos;</span>);
socket.on(<span class="hljs-string">&apos;message&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">msg,rinfo</span>)</span>{
    <span class="hljs-built_in">console</span>.log(msg.toString());
    <span class="hljs-built_in">console</span>.log(rinfo);
});
socket.setTTL(<span class="hljs-number">128</span>);
socket.send(<span class="hljs-keyword">new</span> Buffer(<span class="hljs-string">&apos;&#x73E0;&#x5CF0;&#x57F9;&#x8BAD;&apos;</span>),<span class="hljs-number">0</span>,<span class="hljs-number">6</span>,<span class="hljs-number">41234</span>,<span class="hljs-string">&apos;localhost&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err,bytes</span>)</span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;&#x53D1;&#x9001;&#x4E86;&#x4E2A;%d&#x5B57;&#x8282;&apos;</span>,bytes);
});
socket.on(<span class="hljs-string">&apos;error&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">err</span>)</span>{
    <span class="hljs-built_in">console</span>.error(err);
});
</code></pre>
<h3 id="t312.7 &#x5E7F;&#x64AD;">2.7 &#x5E7F;&#x64AD; <a href="#t312.7 &#x5E7F;&#x64AD;"> # </a></h3>
<p>&#x521B;&#x5EFA;&#x4E00;&#x4E2A;UDP&#x670D;&#x52A1;&#x5668;&#x5E76;&#x901A;&#x8FC7;&#x8BE5;&#x670D;&#x52A1;&#x5668;&#x8FDB;&#x884C;&#x6570;&#x636E;&#x7684;&#x5E7F;&#x64AD;</p>
<h4 id="t322.7.1 &#x670D;&#x52A1;&#x5668;">2.7.1 &#x670D;&#x52A1;&#x5668; <a href="#t322.7.1 &#x670D;&#x52A1;&#x5668;"> # </a></h4>
<pre><code class="lang-javascript">let dgram = require(&apos;dgram&apos;);
let server = dgram.createSocket(&apos;udp4);
server.on(&apos;message&apos;,function(msg){
  let buf = new Bufffer(&apos;&#x5DF2;&#x7ECF;&#x63A5;&#x6536;&#x5BA2;&#x6237;&#x7AEF;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&apos;+msg);
  server.setBroadcast(true);
  server.send(buf,0,buf.length,41235,&quot;192.168.1.255&quot;);
});
server.bind(41234,&apos;192.168.1.100&apos;);
</code></pre>
<h4 id="t332.7.2 &#x5BA2;&#x6237;&#x7AEF;">2.7.2 &#x5BA2;&#x6237;&#x7AEF; <a href="#t332.7.2 &#x5BA2;&#x6237;&#x7AEF;"> # </a></h4>
<pre><code class="lang-javascript">let dgram = require(&apos;dgram&apos;);
let client = dgram.createSocket(&apos;udp4);
client.bind(41235,&apos;192.168.1.102);
let buf = new Buffer(&apos;hello&apos;);
client.send(buf,0,buf.length,41234,&apos;192.168.1.100&apos;);
client.on(&apos;message&apos;,function(msg,rinfo){
  console.log(&apos;received : &apos;,msg);
});
</code></pre>
<h3 id="t342.8 &#x7EC4;&#x64AD;">2.8 &#x7EC4;&#x64AD; <a href="#t342.8 &#x7EC4;&#x64AD;"> # </a></h3>
<ul>
<li>&#x6240;&#x8C13;&#x7684;&#x7EC4;&#x64AD;&#xFF0C;&#x5C31;&#x662F;&#x5C06;&#x7F51;&#x7EDC;&#x4E2D;&#x540C;&#x4E00;&#x4E1A;&#x52A1;&#x7C7B;&#x578B;&#x8FDB;&#x884C;&#x903B;&#x8F91;&#x4E0A;&#x7684;&#x5206;&#x7EC4;&#xFF0C;&#x4ECE;&#x67D0;&#x4E2A;socket&#x7AEF;&#x53E3;&#x4E0A;&#x53D1;&#x9001;&#x7684;&#x6570;&#x636E;&#x53EA;&#x80FD;&#x88AB;&#x8BE5;&#x7EC4;&#x4E2D;&#x7684;&#x5176;&#x4ED6;&#x4E3B;&#x673A;&#x6240;&#x63A5;&#x6536;&#xFF0C;&#x4E0D;&#x88AB;&#x7EC4;&#x5916;&#x7684;&#x4EFB;&#x4F55;&#x4E3B;&#x673A;&#x63A5;&#x6536;&#x3002;</li>
<li>&#x5B9E;&#x73B0;&#x7EC4;&#x64AD;&#x65F6;&#xFF0C;&#x5E76;&#x4E0D;&#x76F4;&#x63A5;&#x628A;&#x6570;&#x636E;&#x53D1;&#x9001;&#x7ED9;&#x76EE;&#x6807;&#x5730;&#x5740;&#xFF0C;&#x800C;&#x662F;&#x5C06;&#x6570;&#x636E;&#x53D1;&#x9001;&#x5230;&#x7EC4;&#x64AD;&#x4E3B;&#x673A;&#xFF0C;&#x64CD;&#x4F5C;&#x7CFB;&#x7EDF;&#x5C06;&#x628A;&#x8BE5;&#x6570;&#x636E;&#x7EC4;&#x64AD;&#x7ED9;&#x7EC4;&#x5185;&#x7684;&#x5176;&#x4ED6;&#x6240;&#x6709;&#x6210;&#x5458;&#x3002;</li>
<li>&#x5728;&#x7F51;&#x7EDC;&#x4E2D;&#xFF0C;&#x4F7F;&#x7528;D&#x7C7B;&#x5730;&#x5740;&#x4F5C;&#x4E3A;&#x7EC4;&#x64AD;&#x5730;&#x5740;&#x3002;&#x8303;&#x56F4;&#x662F;&#x6307; 224.0.0.0 ~ 239.255.255.255,&#x5206;&#x4E3A;&#x4E09;&#x7C7B;<ul>
<li>&#x5C40;&#x90E8;&#x7EC4;&#x64AD;&#x5730;&#x5740;: 224.0.0.0 ~ 224.0.0.255 &#x4E3A;&#x8DEF;&#x7531;&#x534F;&#x8BAE;&#x548C;&#x5176;&#x4ED6;&#x7528;&#x9014;&#x4FDD;&#x7559;</li>
<li>&#x9884;&#x7559;&#x7EC4;&#x64AD;&#x5730;&#x5740;: 224.0.1.0 ~ 238.255.255.255 &#x53EF;&#x7528;&#x4E8E;&#x5168;&#x7403;&#x8303;&#x56F4;&#x6216;&#x7F51;&#x7EDC;&#x534F;&#x8BAE;</li>
<li>&#x7BA1;&#x7406;&#x6743;&#x9650;&#x7EC4;&#x64AD;&#x5730;&#x5740; &#xFF1A; 239.0.0.0 &#xFF5E; 239.255.255.255 &#x7EC4;&#x7EC7;&#x5185;&#x90E8;&#x4F7F;&#x7528;&#xFF0C;&#x4E0D;&#x53EF;&#x7528;&#x4E8E;Internet</li>
</ul>
</li>
</ul>
<p>&#x628A;&#x8BE5;socket&#x7AEF;&#x53E3;&#x5BF9;&#x8C61;&#x6DFB;&#x52A0;&#x5230;&#x7EC4;&#x64AD;&#x7EC4;&#x4E2D;&#x3002;</p>
<pre><code class="lang-javascript">socket.addMembership(multicastAddress,[multicastInterface]);
</code></pre>
<ul>
<li>multicastAddress &#x5FC5;&#x987B;&#x6307;&#x5B9A;&#xFF0C;&#x9700;&#x8981;&#x52A0;&#x5165;&#x7684;&#x7EC4;&#x64AD;&#x7EC4;&#x5730;&#x5740;</li>
<li>multicastInterface &#x53EF;&#x9009;&#x53C2;&#x6570;&#xFF0C;&#x9700;&#x8981;&#x52A0;&#x5165;&#x7684;&#x7EC4;&#x64AD;&#x7EC4;&#x5730;&#x5740;</li>
</ul>
<pre><code class="lang-javascript">socket.dropMembership(multicastAddress,[multicastInterface]);
socket.setMulticastTTL(ttl);
socket.setMulticastLoopback(flag);
</code></pre>
<h4 id="t352.8.1 &#x670D;&#x52A1;&#x5668;">2.8.1 &#x670D;&#x52A1;&#x5668; <a href="#t352.8.1 &#x670D;&#x52A1;&#x5668;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> dgram = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;dgram&apos;</span>);
<span class="hljs-keyword">let</span> server = dgram.createSocket(<span class="hljs-string">&apos;udp4&apos;</span>);
server.on(<span class="hljs-string">&apos;listening&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
  server.MulticastTTL(<span class="hljs-number">128</span>);
  server.setMulticastLoopback(<span class="hljs-literal">true</span>);
  server.addMembership(<span class="hljs-string">&apos;230.185.192.108&apos;</span>);
});
setInterval(broadcast,<span class="hljs-number">1000</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">broadcast</span>(<span class="hljs-params"></span>)</span>{
  <span class="hljs-keyword">let</span> buffer = Buffer.from(<span class="hljs-keyword">new</span> <span class="hljs-built_in">Date</span>().toLocaleString());
  server.send(buffer,<span class="hljs-number">0</span>,buffer.length,<span class="hljs-number">8080</span>,<span class="hljs-string">&quot;230.185.192.108&quot;</span>);
}
</code></pre>
<h4 id="t362.8.2 &#x5BA2;&#x6237;&#x7AEF;">2.8.2 &#x5BA2;&#x6237;&#x7AEF; <a href="#t362.8.2 &#x5BA2;&#x6237;&#x7AEF;"> # </a></h4>
<pre><code class="lang-javascript"><span class="hljs-keyword">let</span> dgram = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;dgram&apos;</span>);
<span class="hljs-keyword">let</span> client = dgram.createSocket(<span class="hljs-string">&apos;udp4&apos;</span>);
client.on(<span class="hljs-string">&apos;listening&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>)</span>{
    client.addMembership(<span class="hljs-string">&apos;230.185.192.108&apos;</span>);
});
client.on(<span class="hljs-string">&apos;message&apos;</span>,<span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">message,remote</span>)</span>{
  <span class="hljs-built_in">console</span>.log(message.toString());
});
client.bind(<span class="hljs-number">8080</span>,<span class="hljs-string">&apos;192.168.1.103&apos;</span>);
</code></pre>
<h2 id="t37&#x9644;&#x5F55;">&#x9644;&#x5F55; <a href="#t37&#x9644;&#x5F55;"> # </a></h2>
<h3 id="t38&#x8F6F;&#x4EF6;">&#x8F6F;&#x4EF6; <a href="#t38&#x8F6F;&#x4EF6;"> # </a></h3>
<ul>
<li>putty <a href="https://www.putty.org/">https://www.putty.org/</a> </li>
<li>window&#x7528;&#x6237;&#x88C5; &#x79D1;&#x6765;&#x7F51;&#x7EDC;&#x5206;&#x6790;&#x7CFB;&#x7EDF; <a href="http://www.colasoft.com.cn/">http://www.colasoft.com.cn/</a></li>
<li>mac&#x7528;&#x6237;&#x88C5; wireshark <a href="https://www.wireshark.org/download.html">https://www.wireshark.org/download.html</a></li>
</ul>
<h3 id="t39telnet">telnet <a href="#t39telnet"> # </a></h3>
<pre><code>FF FB 1F FF FB 20 FF FB 18 FF FB 27 FF FD 01 FF FB 03 FF FD 03
FF FB 1F window size
FF FB 20 terminal speed
FF FB 18 terminal type
FF FB 27 Telnet Environment Option
FF FD 01 echo
FF FB 03 suppress go ahead
FF FD 03 suppress go ahead
&#x5982;&#x679C;&#x4E0D;&#x9700;&#x8981;&#x9019;&#x4E9B;, &#x6539;&#x7528;RAW&#x6A21;&#x5F0F;&#x5C31;&#x53EF;&#x4EE5;&#x4E86;
</code></pre><h2 id="t40&#x6253;&#x5F00;telnet&#x529F;&#x80FD;">&#x6253;&#x5F00;telnet&#x529F;&#x80FD; <a href="#t40&#x6253;&#x5F00;telnet&#x529F;&#x80FD;"> # </a></h2>
<p><a href="https://jingyan.baidu.com/article/6181c3e0b7cc4a152ef153b4.html">&#x6253;&#x5F00;telnet&#x529F;&#x80FD;</a></p>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
